//
//  MainViewController.m
//  PPScaleDemo
//
//  Created by 彭思远 on 2021/3/10.
//

#import "MainViewController.h"
#import "UserInformationEditingViewController.h"
#import "FunctionViewController.h"

@interface MainViewController ()

@property (nonatomic, strong) UserInformationEditingViewController *userInfo;
@property (nonatomic, strong) UINavigationController *function;
@property (nonatomic, strong) IdentityManager *identityManager;
@end

@implementation MainViewController

#pragma mark - Public Func
#pragma mark - Life Cycle
#pragma mark - Private Func
#pragma mark - Actions
#pragma mark - Delegate
#pragma mark - Setter
#pragma mark - Getter
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.identityManager = [IdentityManager manager];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(gotoFunctionVC:) name:@"userCreatSuccess" object:nil];
    [self gotoFunctionVC:nil];
}


- (void)gotoFunctionVC:(NSNotification *)notification {
    LFWeakSelf(self)
    
    if(self.identityManager.currentUser == nil) {
        self.userInfo = [UserInformationEditingViewController instance];
        self.userInfo.view.alpha = 0;
        [self addChildViewController:self.userInfo];
        [self.view addSubview:self.userInfo.view];
        [UIView animateWithDuration:0.5 animations:^{
            weakself.userInfo.view.alpha = 1;
        }];
        return;
    }

    if(self.identityManager.currentUser) {
        self.function = [[UINavigationController alloc] initWithRootViewController:[FunctionViewController instance]];
        self.function.view.alpha = 0;
        [self addChildViewController:self.function];
        [self.view addSubview:self.function.view];
        //如果是从业务界面进来
        if([self.childViewControllers containsObject:self.userInfo]) {
            [self transitionFromViewController:self.userInfo toViewController:self.function duration:1 options:UIViewAnimationOptionTransitionFlipFromLeft  animations:^{
                weakself.function.view.alpha = 1;
                weakself.userInfo.view.alpha = 0;
            } completion:^(BOOL finished) {
                [weakself.userInfo.view removeFromSuperview];
                [weakself.userInfo removeFromParentViewController];
            }];
        } else {
            [UIView animateWithDuration:0.5 animations:^{
                weakself.function.view.alpha = 1;
            }];
        }
        return;
    }
}

- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



@end
